package deepcopy;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CopyTest {
    public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        @SuppressWarnings("unchecked")
        List<T> dest = (List<T>) in.readObject();
        return dest;
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Teacher teacher1 = new Teacher("高",12);
        Teacher teacher2 = new Teacher("就",18);
        Teacher teacher3 = new Teacher("模",22);
        List<Teacher> teachers = new ArrayList<>();
        teachers.add(teacher1);
        teachers.add(teacher2);
        teachers.add(teacher3);
        List<Teacher> teacherList = new ArrayList<>();
        Teacher teacher4 = new Teacher("高",12);
        Teacher teacher5 = new Teacher("就",18);
        Teacher teacher6 = new Teacher("模",22);
        System.out.println(teacher1.hashCode());
        System.out.println(teacher4.hashCode());
        System.out.println(teacher1.equals(teacher4));
        System.out.println("-----------");
        teacherList.add(teacher4);
        teacherList.add(teacher5);
        teacherList.add(teacher6);
        System.out.println("teacherList的hashCode" + teacherList.hashCode());
        System.out.println("teachers的hashCode" + teachers.hashCode());
        System.out.println(teacherList.equals(teachers));
//        teacherList = deepCopy(teachers);
//        System.out.println("teacherList的hashCode" + teacherList.hashCode());
//        System.out.println("teachers的hashCode" + teachers.hashCode());
//        System.out.println(teacherList.equals(teachers));
//        System.out.println(teacherList);
//        teacherList.get(1).setName("hhhhhhhhhhh");
//        System.out.println("-------------------------------------------");
//        System.out.println(teachers);
//        System.out.println(teacherList);
//        System.out.println("teacherList的hashCode" + teacherList.hashCode());
//        System.out.println("teachers的hashCode" + teachers.hashCode());
    }
}
